/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.enforcement.hid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.enforcement.hid.bo.HidPlanCycleDateBO;
import com.hopes.bizz.enforcement.hid.entity.RiskHidCheckPlanEntity;
import com.hopes.bizz.enforcement.hid.mapper.RiskHidCheckPlanMapper;
import com.hopes.bizz.enforcement.hid.service.RiskHidCheckPlanService;
import com.hopes.boss.common.core.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 隐患检查计划
 *
 * @author daijie
 * @date 2023-07-27 09:57:01
 */
@Service
public class RiskHidCheckPlanServiceImpl extends ServiceImpl<RiskHidCheckPlanMapper, RiskHidCheckPlanEntity> implements RiskHidCheckPlanService {

    @Override
    public boolean save(RiskHidCheckPlanEntity entity) {
        entity.setRunFlag(Boolean.FALSE);
        final HidPlanCycleDateBO hidPlanCycleDateBO = new HidPlanCycleDateBO(entity.getPushCycle(), entity.getPushDate(), entity.getPushTime());
        entity.setNextExecuteDate(hidPlanCycleDateBO.getPlanDate());
        entity.setPlanNo(DateUtils.formatDate(new Date(), DateUtils.FORMAT_SIMPLE_MINUTE));
        return baseMapper.insert(entity) > 0;
    }

    @Override
    public boolean updateById(RiskHidCheckPlanEntity entity) {
        entity.setRunFlag(Boolean.FALSE);
        final HidPlanCycleDateBO hidPlanCycleDateBO = new HidPlanCycleDateBO(entity.getPushCycle(), entity.getPushDate(), entity.getPushTime());
        entity.setNextExecuteDate(hidPlanCycleDateBO.getPlanDate());
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public void startPlan(Long id) {
        RiskHidCheckPlanEntity riskHidCheckPlan = this.getById(id);
        Boolean runFlag = riskHidCheckPlan.getRunFlag();
        if (runFlag) {
            throw new BizException("该计划已启动!");
        }
        this.baseMapper.updateRunFlag(id, Boolean.TRUE);
    }

    @Override
    public void stopPlan(Long id) {
        RiskHidCheckPlanEntity riskHidCheckPlan = this.getById(id);
        Boolean runFlag = riskHidCheckPlan.getRunFlag();
        if (!runFlag) {
            throw new BizException("该计划已停止!");
        }
        this.baseMapper.updateRunFlag(id, Boolean.FALSE);
    }
}
